﻿using System;

using System.Collections.Generic;
using System.Text;
using ScannerApp.Utils;
using System.Collections;
using CodeBetter.Json;

namespace ScannerApp.ScannerWebserviceWrapper
{

    public class TransferStockOrderResult : BaseOrderResult
    {

        public List<ScannedTransferStockGoods> tempTransferInventoryList;
        public List<MergedTransferStockGoods> mergeProducts;

        public List<Product> products;

        public int orderTotalPage;
        public int orderTotalCount;
        private int orderPageIndex;
        private int scanTotalPage;
        private int scanTotalCount;
        private int scanPageIndex;

        public int OrderPageIndex
        {
            get { return orderPageIndex; }
            set { orderPageIndex = value; }
        }

        public int ScanPageIndex
        {
            get { return scanPageIndex; }
            set { scanPageIndex = value; }
        }

        public int ScanTotalPage
        {
            get { return scanTotalPage; }
            set { scanTotalPage = value; }
        }

        public int ScanTotalCount
        {
            get { return scanTotalCount; }
            set { scanTotalCount = value; }
        }

        public int OrderTotalPage
        {
            get { return orderTotalPage; }
            set { orderTotalPage = value; }
        }

        public int OrderTotalCount
        {
            get { return orderTotalCount; }
            set { orderTotalCount = value; }
        }

        public bool IsFinishedStatus()
        {
            return false;
        }

        #region IScanProduct 成员
        public override bool IsProductCodeInOrder(string productCode)
        {

            return true;
        }

        public override bool IsQRInOrder(string barcode)
        {
            return true;
        }

        public override bool IsProductEnablingQR(string productCode)
        {
            if (this.products != null)
            {
                foreach (Product product in this.products)
                {
                    if (product.code.Equals(productCode))
                    {
                        if (product.xlcode == true)
                        {
                            return true;
                        }
                    }
                }
            }

            return false;
        }

        public override bool IsScanQRAfterProduct()
        {
            return false;
        }

        public override bool IsCanScanProductCode(string productCode)
        {
            //TODO:从server端查看此产品码是否开启了QR， 如果是， 应该直接输入QR.
            QueryParameter param = new QueryParameter();
            param.barcodeType = (int)ProductBarcodeType.HIBC_First;
            param.barcode = productCode;

            string strResult = ScannerWebServiceProxy.Instance.queryData(Converter.Serialize(param));
            BaseResult<QueryResult> serviceResult = Converter.Deserialize<BaseResult<QueryResult>>(strResult);
            if (serviceResult.IsSucessful())
            {
                List<Product> products = serviceResult.ReturnValue.products;
                if (products != null && products.Count>0)
                {
                    return !products[0].xlcode;
                }
            }
            return true;
        }


        #endregion

        /*
        public IList<MergedTransferStockGoods> GetMergedProducts()
        {
            IList<MergedTransferStockGoods> result = new List<MergedTransferStockGoods>();
            Hashtable inventorys = new Hashtable();

            foreach (ScannedTransferStockGoods rec in tempTransferInventoryList)
            {
                String key = rec.productCode + "|" + rec.batch;

                if (!inventorys.ContainsKey(key))
                {
                    MergedTransferStockGoods mergedRec = new MergedTransferStockGoods();
                    mergedRec.productCode = rec.productCode;
                    mergedRec.batch = rec.batch;
                    mergedRec.amount = 0;

                    inventorys.Add(key, mergedRec);
                    result.Add(mergedRec);
                }

                MergedTransferStockGoods hashRec = inventorys[key] as MergedTransferStockGoods;
                hashRec.amount += 1;
            }
            return result;
        }
        */
        public bool IsTargetShelf(string originalShelf, string targetShelf)
        {
            return true;
        }

        public int GetUnfinishedCount(string productCode, string lot)
        {
            return 0;
        }

        public bool HasOrderFinished()
        {
            return false;
        }

        public bool IsRepeatedScan(string barcode)
        {
            foreach (ScannedTransferStockGoods rec in this.tempTransferInventoryList)
            {
                if (barcode.Equals(rec.serialNumber) && rec.amount!=0)
                {              
                    return true;
                }
            }

            return false;
        }

    }

    public class MergedTransferStockGoods
    {
        public int id;

        public String positionCode; //调出货位号


        [DataTableFieldAttribute(HeaderText = "产品代码")]
        public String productCode; //产品代码

        public String prdName; //产品名称

        [DataTableFieldAttribute(HeaderText = "批次")]
        public String batch; //批次

        [DataTableFieldAttribute(HeaderText = "数量")]
        public float amount; //当前数量


    }

    public class ScannedTransferStockGoods
    {
        public int id;

        public String positionCode; //调出货位号

        [DataTableFieldAttribute(HeaderText = "序列号")]
        public String serialNumber; 

        [DataTableFieldAttribute(HeaderText = "产品代码")]
        public String productCode; //产品代码
    	
	    public  String prdName; //产品名称

        [DataTableFieldAttribute(HeaderText = "批次")]
        public String batch; //批次

        [DataTableFieldAttribute(HeaderText = "数量")]
        public float amount; //当前数量


    }
    
}
